/**
 * jims
 */
package com.jims.master.modules.operation.web;


import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.operation.entity.OperationRoom;
import com.jims.master.modules.operation.service.OperationRoomService;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.service.OrgDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 手术间Controller
 * @author DT
 * @version 2017-04-06
 */
@Controller
@RequestMapping(value = "${adminPath}/operation/operationRoom")
public class OperationRoomController extends BaseController {

	@Autowired
	private OperationRoomService operationRoomService;
	@Autowired
	private OrgDeptService orgDeptService;
	
	@ModelAttribute
	public OperationRoom get(@RequestParam(required=false) String id) {
		OperationRoom entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationRoomService.get(id);
		}
		if (entity == null){
			entity = new OperationRoom();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(OperationRoom operationRoom, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OperationRoom> page = operationRoomService.findPage(new Page<OperationRoom>(request, response), operationRoom);
		model.addAttribute("page", page);
		return "modules/operation/operationRoomIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(OperationRoom operationRoom, Model model) {
		if(StringUtils.isNotBlank(operationRoom.getDeptId())){
			OrgDept orgDept = new OrgDept();
			orgDept=orgDeptService.get(operationRoom.getDeptId());
			operationRoom.setDeptName(orgDept.getDeptName());
		}
		model.addAttribute("operationRoom", operationRoom);
		return "modules/operation/operationRoomForm";
	}

    @RequiresPermissions("user")
    @RequestMapping(value = "applyIndex")
    public String applyIndex(OperationRoom operationRoom, Model model) {
        OrgDept orgDept=new OrgDept();
        List<OrgDept> list=orgDeptService.findList(orgDept);
        model.addAttribute("list",list);
        List<OperationRoom> operationRoomList = operationRoomService.findList(operationRoom);
        model.addAttribute("operationRoomList",operationRoomList);
        model.addAttribute("operationRoom", operationRoom);
        return "modules/operation/operationRoomApplyIndex";
    }


	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(OperationRoom operationRoom, Model model) {
		int i=0;
	    StringData data=new StringData();
		if(	StringUtils.isBlank(operationRoom.getId())){
			List<OperationRoom> list=operationRoomService.findByRoomNo(operationRoom);
			if(list.size()==0){
				i=operationRoomService.save(operationRoom);
			}
			else {
				i=-1;
			}
		}
		else {
			i=operationRoomService.save(operationRoom);
		}
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }
		else if(i==-1){
			data.setCode("repeat");
			data.setData("您输入的手术间号已存在");
		}
		else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(OperationRoom operationRoom) {
	    StringData data=new StringData();
		int i=operationRoomService.delete(operationRoom);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}